package com.fs.sale.service.impl;

import com.fs.sale.mapper.OutboundDeliveryMapper;
import com.fs.sale.pojo.OutboundDelivery;
import com.fs.sale.service.ObdDeliveryService;
import com.fs.sale.vo.ObdDeliveryVo;
import com.fs.system.mapper.SysUserMapper;
import com.fs.system.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ObdDeliveryServiceImpl implements ObdDeliveryService {
    @Autowired
    OutboundDeliveryMapper obdDeliveryMapper;
    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public List<ObdDeliveryVo> select() {
        List<ObdDeliveryVo> obdDeliveryVoList=obdDeliveryMapper.query();
        for(ObdDeliveryVo obdDeliveryVo:obdDeliveryVoList){
            if(obdDeliveryVo.getUserId()!=null){
                obdDeliveryVo.setUserName(sysUserMapper.selectUserById(obdDeliveryVo.getUserId().longValue()).getUserName());
            }
            setStatusStr(obdDeliveryVo);
        }
        String post = sysUserMapper.selectPostNameByUserId(SecurityUtils.getUserId());
        if(post.equals("普通员工")){
            int userId = SecurityUtils.getUserId().intValue();
            List<ObdDeliveryVo> deliveredItems = obdDeliveryVoList.stream()
                    .filter(item -> userId == item.getUserId() )
                    .collect(Collectors.toList());
            return deliveredItems;
        }

        return obdDeliveryVoList;
    }

    public ObdDeliveryVo setStatusStr(ObdDeliveryVo obdDeliveryVo){
        switch (obdDeliveryVo.getObdStatus()){
            case 0:
                obdDeliveryVo.setStatusStr("待出库");
                break;
            case 1:
                obdDeliveryVo.setStatusStr("部分退货");
                break;
            case 2:
                obdDeliveryVo.setStatusStr("退货");
                break;
            case 3:
                obdDeliveryVo.setStatusStr("已出库");
                break;
        }
   return obdDeliveryVo;
    }

    @Override
    public int insert(OutboundDelivery outboundDelivery) {
       return obdDeliveryMapper.add(outboundDelivery);
    }

    @Override
    public int update(OutboundDelivery outboundDelivery) {
//        QueryWrapper<OutboundDelivery> queryWrapper=new QueryWrapper<>();
//        UpdateWrapper<OutboundDelivery> updateWrapper=new UpdateWrapper<>();
//        queryWrapper.eq("obd_id",outboundDelivery.getObdId());
//        OutboundDelivery outboundDelivery1= obdDeliveryMapper.selectOne(queryWrapper);
//
//        if(outboundDelivery.getObdStatus()!=null){
//            outboundDelivery1.setObdStatus(outboundDelivery.getObdStatus());
//            updateWrapper.eq("obd_id",outboundDelivery.getObdId());
//            return obdDeliveryMapper.update(outboundDelivery1,updateWrapper);
//        }
//        outboundDelivery1.setUserId(outboundDelivery.getUserId());
        int i = obdDeliveryMapper.updateById(outboundDelivery);
        return i;

    }


}
